package com.weixin.paleituo.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.CouponsAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.CouponsBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.LoadingDialogUtils;
import com.weixin.paleituo.util.NetUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;
import com.weixin.paleituo.view.YRecycleview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 优惠券页面
 */

public class CouponsActivity extends BasePersonActivity {

    @BindView(R.id.tv_one_activity_coupons)
    TextView mTvOneActivityCoupons;
    @BindView(R.id.headroot_ativity_coupons)
    LinearLayout mllHeadRoot;
    @BindView(R.id.rl_one_activity_coupons)
    RelativeLayout mRlOneActivityCoupons;
    @BindView(R.id.tv_two_activity_coupons)
    TextView mTvTwoActivityCoupons;

    @BindView(R.id.rl_two_activity_coupons)
    RelativeLayout mRlTwoActivityCoupons;
    @BindView(R.id.tv_thread_activity_coupons)
    TextView mTvThreadActivityCoupons;
    @BindView(R.id.rl_thread_activity_coupons)
    RelativeLayout mRlThreadActivityCoupons;
    @BindView(R.id.yrecycleview_activity_coupons)
    YRecycleview mYrecycleviewActivityCoupons;
    LinearLayout llContentRoot;
    private CouponsAdapter mCouponsAdapter;
    private LoadingPagerHead mLoadingPagerHead;
    List<CouponsBean> mCouponsBeanList=new ArrayList<>();


    private Dialog mLoadingDialog;


    @Override
    protected View getContentView(Context context) {

        View inflate = UIUtils.inflate(CouponsActivity.this, R.layout.activity_coupons);
        llContentRoot=   inflate.findViewById(R.id.ll_contetroot_activity_coupons);

        mLoadingPagerHead = new LoadingPagerHead(this,R.layout.layout_coupons_empty) {
            @Override
            protected void close() {

            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(CouponsActivity.this,R.layout.layout_coupons_yrecycleview);
            }
            @Override
            protected void reLoading() {

            retry();
            }
        };

        llContentRoot.addView(mLoadingPagerHead);
        return inflate;
    }

    /**
     * 重试
     */
    private void retry() {
      final   String token = BaseApplication.sUserInfoBean.token;
      final   String userId = BaseApplication.sUserInfoBean.getUserId();
        Observable<StatusCode<List<CouponsBean>>> couponsObservable =   ApiUtils.getApi().getCouponsList(token, userId, 1, "");
        HttpUtil.getInstance().toSubscribe(couponsObservable, new ProgressSubscriber<List<CouponsBean>>(this) {
            @Override
            protected void _onNext(StatusCode<List<CouponsBean>> t) {
                List<CouponsBean> couponsBeans = t.getData();
                if (couponsBeans!=null && couponsBeans.size()>0){
                    mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                    mCouponsBeanList.addAll(couponsBeans);
                    mCouponsAdapter.notifyDataSetChanged();
                }else {
                    mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                }
            }

            @Override
            protected void _onError(String message) {

                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                mCouponsBeanList.clear();
                mCouponsAdapter.notifyDataSetChanged();
            }
        },"",lifecycleSubject,false,true);

    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, CouponsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initData() {
        super.initData();

        mViewline.setVisibility(View.GONE);
        mTvTitle.setText("优惠券");

        mTvOneActivityCoupons.setTextColor(UIUtils.getColor(R.color.c_29ce7e));
        mYrecycleviewActivityCoupons.setLoadMoreEnabled(false);
        mYrecycleviewActivityCoupons.setReFreshEnabled(false);
        mCouponsBeanList.clear();
        mCouponsAdapter = new CouponsAdapter(this, mCouponsBeanList);
        //设置布局管理器
        mYrecycleviewActivityCoupons.setLayoutManager(new LinearLayoutManager(this));
        //设置adapter
        mYrecycleviewActivityCoupons.setAdapter(mCouponsAdapter);
        //设置Item增加、移除动画
        mYrecycleviewActivityCoupons.setItemAnimator(new DefaultItemAnimator());
        //添加分割线
        //   mYRecycleview.addItemDecoration(new DividerItemDecoration(
        //this, DividerItemDecoration.VERTICAL));
        retry();
    }

    @OnClick(R.id.rl_one_activity_coupons)
    public void onMRlOneActivityCouponsClicked() {
        mTvOneActivityCoupons.setTextColor(UIUtils.getColor(R.color.c_29ce7e));
        mTvTwoActivityCoupons.setTextColor(UIUtils.getColor(R.color.c_ff383838));
        mTvThreadActivityCoupons.setTextColor(UIUtils.getColor(R.color.c_ff383838));

        switchCheck(1);

    }

    /**
     * 选中哪一个
     */
    private void switchCheck(int state) {
        mCouponsAdapter.clearMap();

        if (!NetUtils.NetwrokIsUseful(this)){
            ToastUtils.makeText(R.string.check_internet);
            return;
        }
        if (!BaseApplication.isLoginSuccess){
            LoginActivity.startActivity(this,true);
            ToastUtils.makeText("请登录");
            return;
        }
        //1,未使用 2已使用 3已过期
        Observable<StatusCode<List<CouponsBean>>> observable = ApiUtils.getApi().getCouponsList(BaseApplication.sUserInfoBean.token, BaseApplication.getUserId(), state, "")
                . subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        if (mLoadingDialog == null) {
                            mLoadingDialog = LoadingDialogUtils.createLoadingDialog(CouponsActivity.this, "");
                        }
                        mLoadingDialog.show();
                    }
                }).subscribeOn(AndroidSchedulers.mainThread());
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<List<CouponsBean>>(this) {

            @Override
            protected void _onNext(StatusCode<List<CouponsBean>> t) {

                List<CouponsBean> couponsBeans = t.getData();
                LoadingDialogUtils.closeDialog(mLoadingDialog);

                if (couponsBeans!=null &&couponsBeans.size()>0){
                    mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                    mCouponsBeanList.clear();
                    mCouponsBeanList.addAll(couponsBeans);
                    mCouponsAdapter.notifyDataSetChanged();
                }else {
                    mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                }
            }

            @Override
            protected void _onError(String message) {
                LoadingDialogUtils.closeDialog(mLoadingDialog);
                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                mCouponsBeanList.clear();
                mCouponsAdapter.notifyDataSetChanged();

            }
        },"",lifecycleSubject,false,true);
    }

    @OnClick(R.id.rl_two_activity_coupons)
    public void onMRlTwoActivityCouponsClicked() {
        mTvOneActivityCoupons.setTextColor(UIUtils.getColor(R.color.c_ff383838));
        mTvTwoActivityCoupons.setTextColor(UIUtils.getColor(R.color.c_29ce7e));
        mTvThreadActivityCoupons.setTextColor(UIUtils.getColor(R.color.c_ff383838));

        switchCheck(2);
    }


    @OnClick(R.id.rl_thread_activity_coupons)
    public void onMRlThreadActivityCouponsClicked() {
        mTvOneActivityCoupons.setTextColor(UIUtils.getColor(R.color.c_ff383838));
        mTvTwoActivityCoupons.setTextColor(UIUtils.getColor(R.color.c_ff383838));
        mTvThreadActivityCoupons.setTextColor(UIUtils.getColor(R.color.c_29ce7e));

        switchCheck(3);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    
}
